package com.information.manage.basic.dataOperator;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.information.manage.basic.model.entity.DepartmentDO;
import com.information.manage.basic.model.entity.RelStaffDepDO;
import com.information.manage.basic.model.entity.StaffDO;
import com.information.manage.basic.common.recycling.RecoveryDataOperateManager;
import com.information.manage.basic.common.recycling.RecoveryDataOperator;
import com.information.manage.basic.common.recycling.RecoveryException;
import com.information.manage.basic.mapper.RelStaffDepMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Component
public class RelStaffDepDataOperator implements RecoveryDataOperator {

    @Resource
    private RelStaffDepMapper relStaffDepMapper;

    @Resource
    private RecoveryDataOperateManager recoveryDataOperateManager;

    @PostConstruct
    private void init(){
        recoveryDataOperateManager.addDataOperate(this);
    }

    @Override
    public void deleteData(Class entityClass, List<Integer> ids) {
        if (entityClass.equals(DepartmentDO.class)){  //部门 判断能否删除  当员工部门中间表存在数据时 表示该部门下存在员工 则不能删除 返回false
            Integer count = relStaffDepMapper.selectCount(new LambdaQueryWrapper<RelStaffDepDO>().in(RelStaffDepDO::getDepartmentId, ids));
            if (count != 0) throw new RecoveryException("该部门下存在员工，不能删除");
        }else {  //员工 删除
            Integer count = relStaffDepMapper.delete(new LambdaQueryWrapper<RelStaffDepDO>().in(RelStaffDepDO::getStaffId, ids));
            if (!count.equals(ids.size())) throw new RecoveryException("员工部门中间表删除失败，员工ids = " + ids);
        }
    }

    @Override
    public List<Class> dependencies() {
        return Arrays.asList(DepartmentDO.class, StaffDO.class);
    }

    @Override
    public Class getEntityClass() {
        return RelStaffDepDO.class;
    }
}
